import numpy as np
import cv2
import pydetector


def detect_video(video_file):
    cap = cv2.VideoCapture(video_file)
    assert cap.isOpened()
    face_detector =  pydetector.FaceDetector(model_path='./models', num_thread=2, scale=0.05)
    meter = cv2.TickMeter()
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        # frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
        meter.reset()
        meter.start()
        boxes = face_detector.detect(img_bgr=frame)
        meter.stop()
        for item in boxes:
            cv2.rectangle(frame, (item.x, item.y), (item.x + item.width, item.y + item.height), (0, 255, 255), 2)
        cv2.imshow('face-detect', frame)
        print('time={:.3}ms'.format(meter.getTimeMilli()))
        cv2.waitKeyEx(33)


if __name__ == '__main__':
    detect_video(video_file='/home/wei/videos/VID_20200220_122310.mp4')